package com.gszy.auth.utils;

import org.apache.taglibs.standard.extra.spath.Step;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.function.Function;

public class StringUtils {

    public static String getMd5Encode(String password) throws NoSuchAlgorithmException {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        byte[] digest = md5.digest(password.getBytes());
        byte[] encode = Base64.getEncoder().encode(digest);

        return new String(encode);

    }



    public static void main(String[] args) throws NoSuchAlgorithmException{
        String md5Encode = getMd5Encode("123456");
        System.out.println(md5Encode);

    }

    /**
     * 将下划线转为驼峰命名变量：sys_user -> sysUser
     */
    public static String toCamelCase(String columnName) {
        String[] words = columnName.split("_");
        columnName = "";
        for (int i = 0; i < words.length; i++) {
            String word = words[i];
            if (i != 0) {
                word = toUpperCaseFirstOne(word);
            }
            columnName += word;
        }
        return columnName;
    }

    /**
     * 首字母大写
     */
    public static String toUpperCaseFirstOne(String s) {
        return changeCharacterFirstOne(s,(x) ->{
            return Character.toUpperCase(x.charAt(0));
        });

    }

    /**
     * 首字母小写
     * @param s
     * @return
     */
    public static String toLowerCaseFirstOne(String s) {
        return changeCharacterFirstOne(s,(x) ->{
        return Character.toLowerCase(x.charAt(0));
        });
    }
    public static String changeCharacterFirstOne(String s, Function<String,Character> changeType){
//        if (Character.isUpperCase(s.charAt(0))) {
//            return s;
//        } else {
            return (new StringBuilder()).append(changeType.apply(s)).append(s.substring(1)).toString();
//        }

    }
}
